package com.mxchip.livestar.ui.call

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer

class DbyNetworkViewModel(application: Application) : AndroidViewModel(application) {

    private val _delay: MutableLiveData<Int> = MutableLiveData()

    val delay: LiveData<Int>
        get() = _delay

    fun setDelay(i: Int) {
        _delay.value = i
    }

    private val _speed: MutableLiveData<String> = MutableLiveData()

    val speed: LiveData<String>
        get() = _speed

    fun setSpeed(i: String) {
        _speed.value = i
    }


    private val _txQuality: MutableLiveData<Int> = MutableLiveData()

    val txQuality: LiveData<Int>
        get() = _txQuality

    fun setTxQuality(i: Int) {
        _txQuality.value = i
    }

    private val _rxQuality: MutableLiveData<Int> = MutableLiveData()

    val rxQuality: LiveData<Int>
        get() = _rxQuality

    fun setRxQuality(i: Int) {
        _rxQuality.value = i
    }

    val weakNet = MediatorLiveData<Int>().apply {
        value = 0
    }


    private val weakNetObserver = Observer<Any> {
        if (txQuality.value in 5..6 || rxQuality.value in 5..6) {
            weakNet.value = 5
        }

    }

    init {
        weakNet.addSource(txQuality, weakNetObserver)
        weakNet.addSource(rxQuality, weakNetObserver)
    }

}


